package com.cy.ppj.service.company.impl;

import com.cy.ppj.dao.mapper.company.CompanyMapper;
import com.cy.ppj.dao.mapper.company.CompanyMemberMapper;
import com.cy.ppj.dao.mapper.user.UserMapper;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.ao.company.CompanyAddAO;
import com.cy.ppj.model.ao.company.CompanyEditAO;
import com.cy.ppj.model.ao.user.CompanyMemberAddAO;
import com.cy.ppj.model.domian.company.Company;
import com.cy.ppj.model.domian.company.CompanyMember;
import com.cy.ppj.model.domian.user.User;
import com.cy.ppj.model.enums.CompanyStatus;
import com.cy.ppj.model.enums.UserType;
import com.cy.ppj.model.query.CompanyAdminQuery;
import com.cy.ppj.model.query.CompanyTeamMemberAdminQuery;
import com.cy.ppj.model.vo.CompanyAdminVO;
import com.cy.ppj.model.vo.admin.company.CompanyTeamMemberAdminVO;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.company.CompanyService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description: 公司
 * @author: Ocean
 * @create: 2018-08-03 17:51
 **/
@Service
public class CompanyServiceImpl extends BaseService implements CompanyService {

    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CompanyMemberMapper companyMemberMapper;

    @Override
    @Transactional
    public void addCompany(CompanyAddAO companyAddAO) {
    	//验证公司编号是否已存在
    	Company dbCompany = new Company();
    	dbCompany.setNo(companyAddAO.getNo());
    	int count = companyMapper.selectCount(dbCompany);
    	state(count != 1, "该公司编号已存在");
    	//验证公司名称是否已存在
    	dbCompany.setName(companyAddAO.getName());
    	dbCompany.setNo(null);
    	count = companyMapper.selectCount(dbCompany);
    	state(count != 1, "该公司名称已存在");
    	//新增公司
        Company insertCompany = new Company();
        BeanUtils.copyProperties(companyAddAO, insertCompany);
        
        if (!StringUtils.isEmpty(companyAddAO.getSalesMobile())) {
            User user = new User();
            user.setMobile(companyAddAO.getSalesMobile());
            user.setType(UserType.SALES);
            user = userMapper.selectOne(user);
            state(user != null, "该业务员不存在");
            insertCompany.setHandlerId(user.getId());
        }

        insertCompany.setCreateTime(new Date());
        companyMapper.insertSelective(insertCompany);
    }

	@Override
	@Transactional
	public void updateCompany(CompanyEditAO companyEditAO) {
		Company updateCompany = new Company();
        BeanUtils.copyProperties(companyEditAO, updateCompany);
        updateCompany.setId(companyEditAO.getCompanyId());
        if (!StringUtils.isEmpty(companyEditAO.getSalesMobile())) {
            User user = new User();
            user.setMobile(companyEditAO.getSalesMobile());
            user.setType(UserType.SALES);
            user = userMapper.selectOne(user);
            state(user != null, "该业务员不存在");
            updateCompany.setHandlerId(user.getId());
        }
        updateCompany.setUpdateTime(new Date());
        companyMapper.updateByPrimaryKeySelective(updateCompany);
	}
    
	@Override
	public PageBean<CompanyAdminVO> selectCompanyAdminList(CompanyAdminQuery query) {
		int total = companyMapper.selectCompanyAdminListCount(query);
		List<CompanyAdminVO> list = new ArrayList<>();
		if(total > 0) {
			query.startPage();
			list = companyMapper.selectCompanyAdminList(query);
		}
		return new PageBean<>(list, total);
	}

	@Override
	@Transactional
	public void updateCompanyStatus(Long companyId, CompanyStatus companyStatus) {
		Company company = new Company();
		company.setId(companyId);
		company.setStatus(companyStatus);
		company.setUpdateTime(new Date());
		int result = companyMapper.updateByPrimaryKeySelective(company);
		state(result == 1, "更改公司状态失败");
	}

	@Override
	@Transactional
	public void setDirector(Long companyId, Long channelUserId) {
		Company company = new Company();
		company.setId(companyId);
		company.setDirectorId(channelUserId);
		company.setUpdateTime(new Date());
		int result = companyMapper.updateByPrimaryKeySelective(company);
		state(result == 1, "设置公司管理员失败");
	}

	@Override
	public List<Company> selectAllCompanyListInCache() {
		Company company = new Company();
		company.setStatus(CompanyStatus.ENABLE);
		List<Company> list = companyMapper.select(company);
		list.stream().forEach(c -> {
			c.setCollectCommission(null);
			c.setContact(null);
			c.setContactMobile(null);
			c.setCreateTime(null);
			c.setDirectorId(null);
			c.setHandlerId(null);
			c.setStatus(null);
			c.setUpdateTime(null);
		});
		return list;
	}

	@Override
	@Transactional
	public void addMemberUser(CompanyMemberAddAO companyMemberAddAO) {
		User dbUser = new User();
		dbUser.setMobile(companyMemberAddAO.getMobile());
		dbUser = userMapper.selectOne(dbUser);
		state(dbUser != null, "该用户未注册");
		
		CompanyMember dbCompanyMember = new CompanyMember();
		dbCompanyMember.setUserId(dbUser.getId());
		dbCompanyMember = companyMemberMapper.selectOne(dbCompanyMember);
		state(dbCompanyMember == null, "该用户已有所属渠道公司，请勿重复绑定");
		
		// 新增绑定公司关系
		CompanyMember companyMember = new CompanyMember();
        companyMember.setUserId(dbUser.getId());
        companyMember.setCompanyId(companyMemberAddAO.getCompanyId());
        companyMember.setCreateTime(new Date());
        companyMemberMapper.insertSelective(companyMember);
		
	}

	@Override
	public PageBean<CompanyTeamMemberAdminVO> selectCompanyTeamMemberAdminList(CompanyTeamMemberAdminQuery query) {
		int total = companyMapper.selectCompanyTeamMemberAdminCount(query);
		List<CompanyTeamMemberAdminVO> list = new ArrayList<>();
		if(total > 0) {
			query.startPage();
			list = companyMapper.selectCompanyTeamMemberAdminList(query);
		}
		
		return new PageBean<>(list, total);
	}

}
